from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse
from django.utils import timezone
import markdown
import re
from django.utils.html import strip_tags


# Create your models here.
# 

"""自建User"""
class UserExtension(models.Model):
    user = models.OneToOneField(User,on_delete=models.CASCADE,related_name='extension')
    password = models.CharField('密码', max_length=20,blank=False,default="233")
    name = models.CharField('名字', max_length=50,blank=False,default="")
    qqnumber = models.CharField('QQ', max_length=50,default="-1")
    email = models.EmailField('邮箱',blank=False,default="")
    url = models.URLField('网址', blank=True,default="https://baidu.com")
    picurl = models.URLField('头像',blank=True,default="https://ling_boogie.gitee.io/peasonal_pic/default_use.png")
    #text = models.TextField('内容')
    created_time = models.DateTimeField('创建时间', default=timezone.now)
    
    class Meta:
        verbose_name = '好兄弟'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

"""文章分类"""
class Category(models.Model):
    name = models.CharField(max_length=100)

    class Meta:
        verbose_name = '分类'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

"""文章标签"""
class Tag(models.Model):
    name = models.CharField(max_length=100)

    class Meta:
        verbose_name = '标签'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Post(models.Model):
    title = models.CharField('标题', max_length=70)
    body = models.TextField('正文')
    #设定创建时间为当前时间
    created_time = models.DateTimeField('创建时间', default=timezone.now)
    modified_time = models.DateTimeField('修改时间')
    excerpt = models.CharField('摘要', max_length=300, blank=True)
    pic = models.CharField('Pic_url',max_length=300,blank=True,default="https://ling_boogie.gitee.io/peasonal_pic/indexBackground.png")
    views = models.PositiveIntegerField(default=0, editable=True)

    tags = models.ManyToManyField(Tag, verbose_name='标签', blank=True)
    category = models.ForeignKey(Category, verbose_name='分类', on_delete=models.CASCADE)
    author = models.ForeignKey(User, verbose_name='作者', on_delete=models.CASCADE)

    class Meta:
        verbose_name = '文章'
        verbose_name_plural = verbose_name

    def __str__(self):
        self.modified_time = timezone.now()
        return self.title
    #设定修改时间为保存时的时间
    def save(self, *args, **kwargs):
        self.modified_time = timezone.now()
        # 首先实例化一个 Markdown 类，用于渲染 body 的文本。
        # 由于摘要并不需要生成文章目录，所以去掉了目录拓展。
        md = markdown.Markdown(extensions=[
            'markdown.extensions.extra',
            'markdown.extensions.codehilite',
        ])
        # 先将 Markdown 文本渲染成 HTML 文本
        # strip_tags 去掉 HTML 文本的全部 HTML 标签
        # 从文本摘取前 54 个字符赋给 excerpt
        self.excerpt = strip_tags(md.convert(self.body))[:200]
        self.excerpt+="..."
        super().save(*args, **kwargs)

    def get_absolute_url(self):
        """逆向解析"""
        return reverse('blog:detail',kwargs={'pk': self.pk})
        
    def increase_views(self):
        self.views += 1
        self.save(update_fields=['views'])


class Friend(models.Model):
    friend_url = models.CharField('链接', max_length=100)
    name = models.CharField(max_length=100)
    pic = models.CharField('Pic_url',max_length=300,default="https://ling_boogie.gitee.io/peasonal_pic/test_1.jpg")
    excerpt = models.TextField('摘要', max_length=300, blank=True)
    #设定创建时间为当前时间
    created_time = models.DateTimeField('创建时间', default=timezone.now)
    class Meta:
        verbose_name = '友链'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.name